﻿using System;
using System.Data;
using System.Data.SqlClient;
using DBModule;

namespace DataLogical.TableClass
{
    #region tblApplicationPresent
    /// <summary>
    /// This object represents the properties and methods of a tblApplicationPresent.
    /// </summary>

    public class tblApplicationPresent
    {
        protected int _iD;
        protected int _applicant;
        protected Nullable<int> _approver;
        protected Nullable<int> _gaUser;
        protected string _checkListType = String.Empty;
        protected Nullable<int> _checkListID;
        protected Nullable<DateTime> _excuteDate;
        protected string _opposite = String.Empty;
        protected string _status = String.Empty;
        protected string _remarks = String.Empty;
        protected string _present = String.Empty;
        protected string _intention = String.Empty;
        protected decimal _averageAmount;
        protected int _numOfPerson;
        protected decimal _totalAmount;
        protected decimal _actualFee;

        public tblApplicationPresent()
        {
        }

        public tblApplicationPresent(int ID)
        {
            SqlModule db = CommonVar.s_SqlModule;
            string strSql = string.Format("SELECT ID,Applicant,Approver,GaUser,CheckListType,CheckListID,ExcuteDate,Opposite,Status,Remarks,Present,Intention,AverageAmount,NumOfPerson,TotalAmount,ActualFee FROM tblApplicationPresent WHERE ID = {0}", ID);
            SqlDataReader reader = db.FastQueryBySql(strSql);
            if (reader.Read())
            {
                this.LoadFromReader(reader);
                reader.Close();
            }
            else
            {
                if (!reader.IsClosed) reader.Close();
                throw new ApplicationException("tblApplicationPresent does not exist.");
            }
        }

        public tblApplicationPresent(SqlDataReader reader)
        {
            this.LoadFromReader(reader);
        }

        protected void LoadFromReader(SqlDataReader reader)
        {
            if (reader != null && !reader.IsClosed)
            {
                _iD = reader.GetInt32(0);
                if (!reader.IsDBNull(1)) _applicant = reader.GetInt32(1);
                if (!reader.IsDBNull(2)) _approver = reader.GetInt32(2);
                if (!reader.IsDBNull(3)) _gaUser = reader.GetInt32(3);
                if (!reader.IsDBNull(4)) _checkListType = reader.GetString(4);
                if (!reader.IsDBNull(5)) _checkListID = reader.GetInt32(5);
                if (!reader.IsDBNull(6)) _excuteDate = reader.GetDateTime(6);
                if (!reader.IsDBNull(7)) _opposite = reader.GetString(7);
                if (!reader.IsDBNull(8)) _status = reader.GetString(8);
                if (!reader.IsDBNull(9)) _remarks = reader.GetString(9);
                if (!reader.IsDBNull(10)) _present = reader.GetString(10);
                if (!reader.IsDBNull(11)) _intention = reader.GetString(11);
                if (!reader.IsDBNull(12)) _averageAmount = reader.GetDecimal(12);
                if (!reader.IsDBNull(13)) _numOfPerson = reader.GetInt32(13);
                if (!reader.IsDBNull(14)) _totalAmount = reader.GetDecimal(14);
                if (!reader.IsDBNull(15)) _actualFee = reader.GetDecimal(15);
            }
        }

        public void Delete(int ID)
        {
            SqlModule db = CommonVar.s_SqlModule;
            string strSql = string.Format("Delete From tblApplicationPresent Where ID={0}", ID);
            db.ExecSql(strSql);
        }

        public DataTable GetAll()
        {
            string strSql = "SELECT ID,Applicant,Approver,GaUser,CheckListType,CheckListID,ExcuteDate,Opposite,Status,Remarks,Present,Intention,AverageAmount,NumOfPerson,TotalAmount,ActualFee FROM tblApplicationPresent";
            SqlModule db = CommonVar.s_SqlModule;
            return db.QueryBySql(strSql);
        }

        public int Insert(tblApplicationPresent obj)
        {
            int rtn = 0;
            string strSql = "Insert Into tblApplicationPresent(Applicant,Approver,GaUser,CheckListType,CheckListID,ExcuteDate,Opposite,Status,Remarks,Present,Intention,AverageAmount,NumOfPerson,TotalAmount,ActualFee)Values(@Applicant,@Approver,@GaUser,@CheckListType,@CheckListID,@ExcuteDate,@Opposite,@Status,@Remarks,@Present,@Intention,@AverageAmount,@NumOfPerson,@TotalAmount,@ActualFee);SELECT @@IDENTITY";
            SqlModule db = CommonVar.s_SqlModule;
            SqlModule.ParameterCollection Params = CreateParams(obj);
            DataTable dt = db.QueryBySql(strSql, Params);
            if (dt.Rows.Count == 1)
            {
                rtn = int.Parse(dt.Rows[0][0] + "");
            }
            return rtn;
        }

        public void Update(tblApplicationPresent obj)
        {
            string strSql = string.Format("Update tblApplicationPresent Set Applicant=@Applicant,Approver=@Approver,GaUser=@GaUser,CheckListType=@CheckListType,CheckListID=@CheckListID,ExcuteDate=@ExcuteDate,Opposite=@Opposite,Status=@Status,Remarks=@Remarks,Present=@Present,Intention=@Intention,AverageAmount=@AverageAmount,NumOfPerson=@NumOfPerson,TotalAmount=@TotalAmount,ActualFee=@ActualFee Where ID={0}", obj.ID);
            SqlModule db = CommonVar.s_SqlModule;
            SqlModule.ParameterCollection Params = CreateParams(obj);
            db.ExecSql(strSql, Params);
        }

        private SqlModule.ParameterCollection CreateParams(tblApplicationPresent obj)
        {
            SqlParameter Param;
            SqlModule.ParameterCollection Result = new SqlModule.ParameterCollection();

            Param = new SqlParameter("@Applicant", SqlDbType.Int);
            Result.Add(Param);
            if (obj.Applicant != null)
            {
                Param.Value = obj.Applicant;
            }
            else
            {
                Param.Value = DBNull.Value;
            }
            Param = new SqlParameter("@Approver", SqlDbType.Int);
            Result.Add(Param);
            if (obj.Approver != null)
            {
                Param.Value = obj.Approver;
            }
            else
            {
                Param.Value = DBNull.Value;
            }
            Param = new SqlParameter("@GaUser", SqlDbType.Int);
            Result.Add(Param);
            if (obj.GaUser != null)
            {
                Param.Value = obj.GaUser;
            }
            else
            {
                Param.Value = DBNull.Value;
            }
            Param = new SqlParameter("@CheckListType", SqlDbType.VarChar);
            Result.Add(Param);
            if (obj.CheckListType != null)
            {
                Param.Value = obj.CheckListType;
            }
            else
            {
                Param.Value = DBNull.Value;
            }
            Param = new SqlParameter("@CheckListID", SqlDbType.Int);
            Result.Add(Param);
            if (obj.CheckListID != null)
            {
                Param.Value = obj.CheckListID;
            }
            else
            {
                Param.Value = DBNull.Value;
            }
            Param = new SqlParameter("@ExcuteDate", SqlDbType.DateTime);
            Result.Add(Param);
            if (obj.ExcuteDate != null)
            {
                Param.Value = obj.ExcuteDate;
            }
            else
            {
                Param.Value = DBNull.Value;
            }
            Param = new SqlParameter("@Opposite", SqlDbType.VarChar);
            Result.Add(Param);
            if (obj.Opposite != null)
            {
                Param.Value = obj.Opposite;
            }
            else
            {
                Param.Value = DBNull.Value;
            }
            Param = new SqlParameter("@Status", SqlDbType.VarChar);
            Result.Add(Param);
            if (obj.Status != null)
            {
                Param.Value = obj.Status;
            }
            else
            {
                Param.Value = DBNull.Value;
            }
            Param = new SqlParameter("@Remarks", SqlDbType.VarChar);
            Result.Add(Param);
            if (obj.Remarks != null)
            {
                Param.Value = obj.Remarks;
            }
            else
            {
                Param.Value = DBNull.Value;
            }
            Param = new SqlParameter("@Present", SqlDbType.VarChar);
            Result.Add(Param);
            if (obj.Present != null)
            {
                Param.Value = obj.Present;
            }
            else
            {
                Param.Value = DBNull.Value;
            }
            Param = new SqlParameter("@Intention", SqlDbType.VarChar);
            Result.Add(Param);
            if (obj.Intention != null)
            {
                Param.Value = obj.Intention;
            }
            else
            {
                Param.Value = DBNull.Value;
            }
            Param = new SqlParameter("@AverageAmount", SqlDbType.Decimal);
            Result.Add(Param);
            if (obj.AverageAmount != null)
            {
                Param.Value = obj.AverageAmount;
            }
            else
            {
                Param.Value = DBNull.Value;
            }
            Param = new SqlParameter("@NumOfPerson", SqlDbType.Int);
            Result.Add(Param);
            if (obj.NumOfPerson != null)
            {
                Param.Value = obj.NumOfPerson;
            }
            else
            {
                Param.Value = DBNull.Value;
            }
            Param = new SqlParameter("@TotalAmount", SqlDbType.Decimal);
            Result.Add(Param);
            if (obj.TotalAmount != null)
            {
                Param.Value = obj.TotalAmount;
            }
            else
            {
                Param.Value = DBNull.Value;
            }
            Param = new SqlParameter("@ActualFee", SqlDbType.Decimal);
            Result.Add(Param);
            if (obj.ActualFee != null)
            {
                Param.Value = obj.ActualFee;
            }
            else
            {
                Param.Value = DBNull.Value;
            }
            return Result;
        }

        public static bool Exist(int ID)
        {
            SqlModule db = CommonVar.s_SqlModule;
            string strSql = string.Format("SELECT COUNT(*) From tblApplicationPresent Where ID={0}", ID);
            return (db.ExecSql_Cnt(strSql) == 1);
        }

        #region Public Properties
        public int ID
        {
            get { return _iD; }
        }

        public int Applicant
        {
            get { return _applicant; }
            set { _applicant = value; }
        }

        public Nullable<int> Approver
        {
            get { return _approver; }
            set { _approver = value; }
        }

        public Nullable<int> GaUser
        {
            get { return _gaUser; }
            set { _gaUser = value; }
        }

        public string CheckListType
        {
            get { return _checkListType; }
            set { _checkListType = value; }
        }

        public Nullable<int> CheckListID
        {
            get { return _checkListID; }
            set { _checkListID = value; }
        }

        public Nullable<DateTime> ExcuteDate
        {
            get { return _excuteDate; }
            set { _excuteDate = value; }
        }

        public string Opposite
        {
            get { return _opposite; }
            set { _opposite = value; }
        }

        public string Status
        {
            get { return _status; }
            set { _status = value; }
        }

        public string Remarks
        {
            get { return _remarks; }
            set { _remarks = value; }
        }

        public string Present
        {
            get { return _present; }
            set { _present = value; }
        }

        public string Intention
        {
            get { return _intention; }
            set { _intention = value; }
        }

        public decimal AverageAmount
        {
            get { return _averageAmount; }
            set { _averageAmount = value; }
        }

        public int NumOfPerson
        {
            get { return _numOfPerson; }
            set { _numOfPerson = value; }
        }

        public decimal TotalAmount
        {
            get { return _totalAmount; }
            set { _totalAmount = value; }
        }

        public decimal ActualFee
        {
            get { return _actualFee; }
            set { _actualFee = value; }
        }
        #endregion
    }
    #endregion
}
